﻿using System;
using System.Drawing;
using FlaUI.Core;
using FlaUI.UIA3;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Input;
using FlaUI.Core.Tools;
using System.IO;

public class ImageHelper
{
    public static string ImageBitmapToBase64(Bitmap bitmap)
    {
        System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Png;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // 将Bitmap对象保存到MemoryStream中  
                bitmap.Save(memoryStream, format);
                byte[] imageBytes = memoryStream.ToArray();
                // 将字节数组转换为Base64字符串  
                string base64String = Convert.ToBase64String(imageBytes);
                // 返回Base64字符串  
                return $"data:image/{format.ToString()};base64,{base64String}";
        }
    }
    public static string ImageUrlToBase64(string imagePath)
    {
        using (Image image = Image.FromFile(imagePath))
        {
            using (MemoryStream m = new MemoryStream())
            {
                // 将图片保存为PNG格式到内存流中  
                // 注意：这里你也可以使用其他格式，比如Jpeg，通过调用image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);  
                image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imageBytes = m.ToArray();
                // 将字节数组转换为Base64字符串  
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
    }

    public static Bitmap CropImage(Bitmap source, Rectangle section)
    {
        section.Width = 36;
        section.Height = 36;
        var bitmap = new Bitmap(section.Width, section.Height);
        using (var graphics = Graphics.FromImage(bitmap))
        {
            graphics.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
        }
        return bitmap;
    }


    public static Bitmap CropImages(Bitmap source, Rectangle section)
    {
        var bitmap = new Bitmap(section.Width+200 , section.Height + 200);
        using (var graphics = Graphics.FromImage(bitmap))
        {
            section.Width = 36 + 200;
            section.Height = 36 + 200;
            graphics.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
        }
        return bitmap;
    }
}